package api.cie.rss.adapters;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import api.cie.rss.R;
import api.cie.rss.common.ParameterKeys;
import api.cie.rss.dal.DatabaseAdapter;
import api.cie.rss.dal.ParameterDAL;

public class SettingsAdapter extends BaseAdapter {

	private ArrayList<HashMap<String,Object>> mData = new ArrayList<HashMap<String,Object>>();
	private LayoutInflater mInflater;
	
	public SettingsAdapter(Context ctx) {
		super();
		mInflater = LayoutInflater.from(ctx);
	}
	
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void addItem(String paramName) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("param", paramName);
		mData.add(map);
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		view = mInflater.inflate(R.layout.settings_row, parent, false);
		TextView tvParam = (TextView)view.findViewById(R.id.textParam);
		TextView tvParamValue = (TextView)view.findViewById(R.id.textParamValue);
		
		tvParam.setText(String.valueOf(mData.get(position).get("param")));
		String value = "";
		
		DatabaseAdapter dbAdapter = new DatabaseAdapter(view.getContext());
		dbAdapter.open();
		ParameterDAL parameterDAL = new ParameterDAL(dbAdapter);
		Cursor cursor = null;
		switch(position) {
		case ParameterKeys.POS_FREQUENCY:
			cursor = parameterDAL.fetchParameterByKey(ParameterKeys.REFRESH_FREQUENCY);
			break;
		case ParameterKeys.POS_SORT:
			cursor = parameterDAL.fetchParameterByKey(ParameterKeys.SORT_BY);
			break;
		case ParameterKeys.POS_FONT:
			cursor = parameterDAL.fetchParameterByKey(ParameterKeys.FONT_SIZE);
			break;
		}
		
		if(cursor != null) value = cursor.getString(cursor.getColumnIndex(ParameterDAL.KEY_PARAMVALUE));
		tvParamValue.setText(value);
		
		dbAdapter.close();
				
		return view;
	}

}
